package FilesGenerator

import javax.swing.ImageIcon
import java.awt.Image
import AndroidProjectFile.{MarketKey, AntSetup, ScalaAndroidSrc, ProjectParameters}

/**
 * Created by IntelliJ IDEA.
 * User: romaincherix
 * Date: 10.04.11
 * Time: 12:43
 * To change this template use File | Settings | File Templates.
 */

object ProjectGenerator {

  //this variable contains the path to access to file
  var localProperties: String = _
  var buildProperties: String = _
  var defaultProperties: String = _


  def createStructure(): Unit = {

    var path = ProjectParameters.projectPath + "/" + ProjectParameters.projectName

    //main folder of project
    FileManager.createDirectory(path)

    //file in root node which contains systems parameters
    buildProperties = FileManager.createFile(path, "build.properties")
    localProperties = FileManager.createFile(path, "local.properties")
    defaultProperties = FileManager.createFile(path, "default.properties")

    //folder in project
    FileManager.createDirectory(path + "/bin")
    FileManager.createDirectory(path + "/configs")
    FileManager.createDirectory(path + "/res/drawable-hdpi")
    FileManager.createDirectory(path + "/res/drawable-ldpi")
    FileManager.createDirectory(path + "/res/drawable-mdpi")
    FileManager.createDirectory(path + "/res/layout")
    FileManager.createDirectory(path + "/res/values")
    FileManager.createDirectory(path + "/libs")

    // src directory
    FileManager.createDirectory(path + "/src", ProjectParameters.packageName)

    // gen directory
    FileManager.createDirectory(path + "/gen", ProjectParameters.packageName)

    //copy toolchain
    FileManager.makeToolChain(path)

    FileManager.writeInFile(path + "/src/" + ProjectParameters.packageName.split('.').mkString("/"),
      ProjectParameters.activityName + ".scala",
      ScalaAndroidSrc.getScalaSrc(ProjectParameters.activityName, ProjectParameters.packageName))



    //create the main interface for Android
    FileManager.copyFile("files/main.xml", path + "/res/layout/main.xml")

    // generate manifest file
    ManifestFile.generate(path)

    //generate String.xml
    val valFile = new ValuesFile
    valFile.addValue("app_name", ProjectParameters.appName)
    valFile.gen(path + "/res/values")

    // generate ant properties
    UserConfig.get
    FileManager.writeInFile(buildProperties, AntSetup.getBuildProperties)
    FileManager.writeInFile(defaultProperties, AntSetup.getDefaultProperties)

    // copy ant-invoked library into Android project
    FileManager copyFile ("files/ant-invoked.jar", path + "/configs/ant-invoked.jar")
    FileManager writeInFile (path + "/configs/", "default-debug.cfg", ProguardGen.getDefaultConfig)
    FileManager writeInFile (path + "/configs/", "default-release.cfg", ProguardGen.getReleaseConfig)

    if(MarketKey.exists){
      MarketKey.load
      FileManager writeInFile (localProperties, AntSetup.getLocalProperties)
    }

    // import icon in project after resizing
    val theIcon = new ImageIcon(ProjectParameters.iconPath) //load icon
    val bigIcon = new ImageIcon(theIcon.getImage().getScaledInstance(74, 74, Image.SCALE_DEFAULT))
    var mediumIcon = new ImageIcon(theIcon.getImage().getScaledInstance(48, 48, Image.SCALE_DEFAULT))
    var smallIcon = new ImageIcon(theIcon.getImage().getScaledInstance(36, 36, Image.SCALE_DEFAULT))

    FileManager saveImage (path + "/res/drawable-hdpi", "icon.png", bigIcon)
    FileManager saveImage (path + "/res/drawable-mdpi", "icon.png", mediumIcon)
    FileManager saveImage (path + "/res/drawable-ldpi", "icon.png", smallIcon)
  }
}